import emEnum from "../em/enumCmds";
import supScripts from "./supScripts"
import explorer from "../api/ts/api_explorer";
import aconsole from "../api/ts/api_console";
import changesc from "../api/ts/api_chagnescreen"
export default class supModule extends supScripts {
    protected static g_supModule: supModule[] = []; //[模块][全局]
    protected static g_sMode: string = null;        //[横屏][竖屏]
    protected sTargetMode: string = null;           //[屏幕]
    protected screen: cc.Node = null;               //场景节点
    protected onself: cc.Node = null;               //场景节点
    protected spanel: string = null;                //预制路径

    //注册模块
    constructor(name: string) {
        super(true, name);
    }


    //获取模块
    public getModule(name: string): any {
        return supScripts.getModule(name);
    }

    //获取挂载
    public getMount(uuid: string): any {
        return supScripts.getMount(uuid ? uuid : this.onself.uuid);
    }

    //面板预制
    public setSpanel(url: string): void {
        this.spanel = url
    }

    public node(): cc.Node {
        return this.onself;
    }

    //指定节点
    public settingnode(index: number): void {
        let nodes = [
            "Canvas/node0",
            "Canvas/node1",
            "Canvas/node2",
            "Canvas/node3",
            "Canvas/node4",
            "Canvas/node5"
        ];
        this.screen = cc.find(nodes[index]);
    }

    //指定节点
    public settingscreen(node: cc.Node): void {
        this.screen = node;
    }

    //竖屏节点
    protected portraitscreen(): void {
        this.sTargetMode = emEnum.prortrait;
    }
    //横屏节点
    protected landscapescreen(): void {
        this.sTargetMode = emEnum.landscape;
    }

    //调整节点
    protected changescreen(): void {
        //没有目标
        if (null == this.sTargetMode) {
            return
        }
        //屏幕模式
        if (supModule.g_sMode != this.sTargetMode) {
            supModule.g_sMode = this.sTargetMode;
            //转成竖屏
            if (this.sTargetMode == emEnum.prortrait) {
                changesc.changeCanvasToVertical()
            }
            //转成横屏
            if (this.sTargetMode == emEnum.landscape) {
                changesc.changeCanvasToHorizontal()
            }
        }
    }



    //实例化
    protected changeInstance(): void {
    }

    protected extendStart(): void {
    }

    protected extendClose(): void {
    }

    //切换
    public changeStart(): void {
        if (!this.onself) {
            explorer.locaLoading(this.spanel, cc.Prefab, null, (error: Error, prefab: any) => {
                if (!error) {
                    this.onself = cc.instantiate(prefab);
                    this.screen.addChild(this.onself);
                    let uuid = this.onself.uuid;
                    supScripts.setModule(uuid, this)
                    this.startEffect(() => {
                        this.changeInstance();
                        this.changescreen();
                        this.startComplete();
                        this.extendStart();
                    });
                }
            });
        }
        else {
            if (!this.onself.active) {
                this.onself.active = true;
                this.startEffect(() => {
                    this.changescreen();
                    this.startComplete();
                    this.extendStart();
                });
            }
        }
    }

    //关闭
    public changeClose(): void {
        let node = this.onself;
        if (node) {
            this.closeEffect(function () {
                node.active = false;
                this.extendClose();
            }.bind(this));
        }
        else {
            aconsole.error("this.onself inexistence")
        }
    }

    //启用
    public startComplete() {
        if (this.sTargetMode) {
            let scenes = supModule.g_supModule
            if (scenes.length > 0) {
                let befor = scenes.pop();
                befor.changeClose();
            }
            scenes.push(this);
        }
    }
    //关闭
    public closeComplete() {
    }

    //启动效果
    public startEffect(cb) {
        cb();
    }

    //关闭效果
    public closeEffect(cb) {
        cb();
    }

    //清除脚本
    public clearScripts(): any {
        supScripts.setMount(null,null);
        supScripts.setModule(null,null);
    }

    //逻辑脚本时间派遣
    public dispatchs(data: any) {

    }
}